package com.ymttest.database.mongo.shenqi;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOptions;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.SellerIndex;
import com.ymttest.database.mongo.config.MongoSellerConfig;
import com.ymttest.database.mongo.config.SchemaField;
import com.ymttest.database.mongo.config.SellerIndexField;
import com.ymttest.database.mongo.config.UserIndexField;
import com.ymttest.utils.DataUtil;

/**
 */
@Repository
public class SellerIndexQueryWapper {

	private MongoTemplate mongoSellerTemplate;

	public SellerIndexQueryWapper() {
		try {
			mongoSellerTemplate = new MongoSellerConfig().mongoSellerTemplate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String getCollectionName(long sellerId) {
		long n = sellerId % 100;
		return SchemaField.SELLER + n;
	}

	public <T> List<T> aggregate(long sellerId, Aggregation aggregation,
			Class<T> outputType) {
		aggregation = aggregation.withOptions(new AggregationOptions.Builder()
				.allowDiskUse(true).build());
		return mongoSellerTemplate.aggregate(aggregation,
				getCollectionName(sellerId), outputType).getMappedResults();
	}

	public <T> List<T> find(Query query, long sellerId, Class<T> outputType) {
		return mongoSellerTemplate.find(query, outputType,
				getCollectionName(sellerId));
	}

	public SellerIndex findByOrderId(long orderId, long sellerId) {

		Query query = new Query(Criteria.where(UserIndexField.ID).is(orderId));

		return mongoSellerTemplate.findOne(query, SellerIndex.class,
				getCollectionName(sellerId));

	}
	public List<Long> queryOrderId(Query query, long sellerId) {
		query.fields().include(SellerIndexField.ID);
		List<SellerIndex> sellerIndexs = mongoSellerTemplate.find(query,
				SellerIndex.class, getCollectionName(sellerId));
		return sellerIndexs.stream().map(e -> e.getId())
				.collect(Collectors.toList());
	}

	public long count(long sellerId, Criteria criteria) {
		return mongoSellerTemplate.count(Query.query(criteria),
				getCollectionName(sellerId));
	}

	public List<SellerIndex> findBySkipLimit(Criteria criteria, long sellerId,
			int skip, int limit, String sort) {
		Query query = Query.query(criteria);

		if (skip > 0) {
			query.skip(skip);
		}
		if (limit > 0) {
			query.limit(limit);
		}
		if (!DataUtil.Stringg.isNullOrEmpty(sort)) {
			query.with(new Sort(Sort.Direction.DESC, sort));
		}
		return mongoSellerTemplate.find(query, SellerIndex.class,
				getCollectionName(sellerId));
	}

	public <T> T findOne(Query query, long sellerId, Class<T> outputType) {
		return mongoSellerTemplate.findOne(query, outputType,
				getCollectionName(sellerId));
	}

	public static void main(String[] args) {

		SellerIndexQueryWapper query = new SellerIndexQueryWapper();
		Criteria criteria = Criteria.where(SellerIndexField.MAIN_ORDER_ID).gte(
				14120983);
		Query q = Query.query(criteria);

		List<Long> list = query.queryOrderId(q, 20346910);
		System.out.println(list);
		List<Integer> ids = Arrays.asList(112469447, 112469464);
		Query q1 = Query.query(Criteria.where(SellerIndexField.ID).in(ids));
		System.out.println(query.queryOrderId(q1, 20346910));

	}
}
